package com.ruoyi.utils.qny;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qiniu.http.Client;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.ruoyi.common.core.domain.AjaxResult;

import java.util.ArrayList;
import java.util.List;

public class QnyOssDomainUtils {


    //创建授权对象的方法
    public static Auth createAuth(String keyId, String keySecret) {
        return Auth.create(keyId, keySecret);
    }

    /**
     * 读取桶域名的方法
     * @param keyId 七牛云keyId
     * @param keySecret 七牛云秘钥
     */
    public static AjaxResult listBucket(String keyId, String keySecret){
        try {
            Auth auth = createAuth(keyId, keySecret);
            StringMap headers = auth.authorizationV2("https://uc.qbox.me/buckets", "GET", "".getBytes(), Client.FormMime);
            HttpRequest request = HttpRequest.get("https://uc.qbox.me/buckets");
            request.header("Content-Type", Client.FormMime);
            request.header("X-Qiniu-Date", headers.map().get("X-Qiniu-Date").toString());
            request.header("Authorization", headers.map().get("Authorization").toString());
            HttpResponse response = request.execute();
            if(response.getStatus() == 200){
                List<String> resultObjList = JSONUtil.toList(response.body(), String.class);
                //返回数据
                return AjaxResult.success("查询七牛云桶成功！", resultObjList);
            }
            return AjaxResult.error("查询七牛云OSS桶失败！");
        } catch (Exception e) {
            return AjaxResult.error("查询七牛云OSS桶失败，"+e.getMessage());
        }
    }

    public static AjaxResult listBucketDomain(String keyId, String keySecret){
        try {
            AjaxResult aj = listBucket(keyId, keySecret);
            if(aj.isSuccess()){
                List<String> bucketList = JSONUtil.toList(JSONUtil.toJsonStr(aj.get("data")), String.class);
                Auth auth = createAuth(keyId, keySecret);
                List<String> bucketDomainList = new ArrayList<>();
                for (String bucket : bucketList) {
                    StringMap headers = auth.authorizationV2("https://uc.qbox.me/v2/domains?tbl="+bucket, "GET", "".getBytes(), Client.FormMime);
                    HttpRequest request = HttpRequest.get("https://uc.qbox.me/v2/domains?tbl="+bucket);
                    request.header("Content-Type", Client.FormMime);
                    request.header("X-Qiniu-Date", headers.map().get("X-Qiniu-Date").toString());
                    request.header("Authorization", headers.map().get("Authorization").toString());
                    HttpResponse response = request.execute();
                    if(response.getStatus() == 200){
                        List<String> resultObjList = JSONUtil.toList(response.body(), String.class);
                        bucketDomainList.addAll(resultObjList);
                    }
                }
                return AjaxResult.success("查询桶域名成功！", bucketDomainList);
            }
            return aj;
        } catch (Exception e) {
            return AjaxResult.error("查询桶域名失败，"+e.getMessage());
        }
    }

    public static void main(String[] args) {
        AjaxResult aj = listBucketDomain("4TkPPBmg0BQmORiTcy81qTPj3_pD3B537pvrUXIu", "WLAmyz9qC34DpEbockQWTZWa-dMGQkJtLXR25mxE");
        Console.log(JSONUtil.toJsonStr(aj));
    }

}
